package com.xzs.demo.util;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.xzs.demo.entity.UserDao;
//@Aspect 
//@Component
public class SessionTimeOutAspect {
/*
	private static Logger logger = LoggerFactory.getLogger(SessionTimeOutAspect.class); 

	public SessionTimeOutAspect() {
	}
	@Pointcut("execution(public * com.xzs.demo.controller..*.*(..))")
	public void controllerPointcut(){
	}
	
	@Pointcut("execution(public * com.xzs.demo.controller.Login.*(..))")
	public void rootPointcut(){//登录登出功能不需要Session验证
	}	
	@Pointcut("controllerPointcut()&&(!rootPointcut())")
	public void sessionTimeOutPointcut(){
	}
	@Around("sessionTimeOutPointcut()")
	public Object sessionTimeOutAdvice(ProceedingJoinPoint pjp) throws IOException {
		Object result = null;
		String targetName = pjp.getTarget().getClass().getSimpleName();
		String methodName = pjp.getSignature().getName();
		System.out.println("----------------执行方法-----------------");
		System.out.println("类名："+targetName+" 方法名："+methodName);
		HttpServletResponse response = null;
		for (Object param : pjp.getArgs()) { 
			if (param instanceof HttpServletResponse) { 
				response = (HttpServletResponse) param;
			}
		}
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();    
		HttpSession session = request.getSession();
		UserDao user = (UserDao) session.getAttribute("user");
		
		if(user==null){
			System.out.println("未登录:"+user);
			 response.sendRedirect(request.getContextPath()+"/logins/log"); 
			 return result;
		}else{
			try {
				System.out.println("已经登录了:"+user);
				result = pjp.proceed();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return result;
		}
		
		
	}	*/
}
